package kr.hkit.powerjava.coffee;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import javax.swing.JButton;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.SQLException;

import kr.hkit.powerjava.coffee.dao.CoffeeDAO;
import kr.hkit.powerjava.coffee.db.ConnectionFactory;
import kr.hkit.powerjava.coffee.dto.SalesRecord;

import javax.swing.JLabel;
import javax.swing.JTextField;

public class FrameEx2 extends JFrame {

	private JTabbedPane contentPane;
	private JLabel lblNewLabel;
	private JPanel panel;
	private JTextField textField;
	private PanelInput2 panel_1;
	private JButton button;
	Connection connnection = ConnectionFactory.getConnection();
	CoffeeDAO dao = new CoffeeDAO(connnection);

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FrameEx2 frame = new FrameEx2();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public FrameEx2() {
		initialize();
	}

	private void initialize() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 228, 318);
		contentPane = new JTabbedPane();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		panel = new JPanel();
		contentPane.addTab("New tab", null, panel, null);

		lblNewLabel = new JLabel("제품코드");
		panel.add(lblNewLabel);
		
		textField = new JTextField();
		textField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					dao.getName(textField.getText().trim());
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		panel.add(textField);
		textField.setColumns(10);
		
		panel_1 = new PanelInput2();
		panel.add(panel_1);
		
		button = new JButton("입력");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String code = textField.getText();
				int price = Integer.parseInt(((PanelInput2) panel).getPdPrice().getTextField()
						.getText().trim());
				int amount = Integer.parseInt(((PanelInput2) panel).getAmount().getTextField()
						.getText().trim());
				double marginrate = Double.parseDouble(((PanelInput2) panel).getMarjinRate()
						.getTextField().getText().trim());
				try {
					dao.insertItem(new SalesRecord(code, price, amount, marginrate));
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		panel.add(button);
	}
}
